package com.yyxk.thousandtime.util;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Locale;

/**
 * Created by liubin on 2017/5/24.
 */

public class FormatUtil {
    /**
     * 格式化预约显示时间
     *
     * @param date date=100000000
     * @return 预约10:00-10:30
     */
    public static String formatOrderedDate(long date) {
        try {
            StringBuilder builder = new StringBuilder();
            SimpleDateFormat sdf = new SimpleDateFormat("预约HH:mm", Locale.CHINA);
            builder.append(sdf.format(date));
            date += 0.5 * 60 * 60 * 1000;
            SimpleDateFormat sdf2 = new SimpleDateFormat("HH:mm", Locale.CHINA);
            builder.append("-").append(sdf2.format(date));
            return builder.toString();
        } catch (Exception e) {
            e.printStackTrace();
            return "";
        }
    }

    public static String formatReceivedDate(long date) {
        try {
            StringBuilder builder = new StringBuilder();
            SimpleDateFormat sdf = new SimpleDateFormat("HH:mm", Locale.CHINA);
            builder.append(sdf.format(date));
            return builder.toString();
        } catch (Exception e) {
            e.printStackTrace();
            return "";
        }
    }

    public static String formatUserOrderedDate(long date) {
        try {
            StringBuilder builder = new StringBuilder();
            SimpleDateFormat sdf = new SimpleDateFormat("用户预约 yyyy年MM月dd日 HH:mm", Locale.CHINA);
            builder.append(sdf.format(date));
            date += 0.5 * 60 * 60 * 1000;
            SimpleDateFormat sdf2 = new SimpleDateFormat("HH:mm", Locale.CHINA);
            builder.append("-").append(sdf2.format(date));
            return builder.toString();
        } catch (Exception e) {
            e.printStackTrace();
            return "";
        }
    }

    public static String formatDate2DateWithWeek(long date) {
        StringBuilder builder = new StringBuilder();
        builder.append("");
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日", Locale.CHINA);
        builder.append(sdf.format(date));
        builder.append(" ").append(getWeek(date));
        if (isDateToday(date))
            builder.append("(今天)");
        return builder.toString();
    }

    public static boolean isDateToday(long date) {
        Calendar current = Calendar.getInstance();
        try {
            //设置当前时间
            current.setTimeInMillis(date);
            //设置今天时间
            Calendar dayBegin = Calendar.getInstance();
            dayBegin.setTimeInMillis(System.currentTimeMillis());
            dayBegin.set(Calendar.HOUR_OF_DAY, 0);
            dayBegin.set(Calendar.MINUTE, 0);
            dayBegin.set(Calendar.SECOND, 0);
            dayBegin.set(Calendar.MILLISECOND, 0);
            // 设置明天时间
            Calendar dayEnd = Calendar.getInstance();
            dayEnd.setTimeInMillis(System.currentTimeMillis());
            dayEnd.set(Calendar.HOUR_OF_DAY, 23);
            dayEnd.set(Calendar.MINUTE, 59);
            dayEnd.set(Calendar.SECOND, 59);
            dayEnd.set(Calendar.MILLISECOND, 999);
            return current.getTimeInMillis() >= dayBegin.getTimeInMillis() &&
                    current.getTimeInMillis() <= dayEnd.getTimeInMillis();
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }

    private static String getWeek(long date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTimeInMillis(date);
        int i = calendar.get(Calendar.DAY_OF_WEEK);
        switch (i) {
            case 1:
                return "星期日";
            case 2:
                return "星期一";
            case 3:
                return "星期二";
            case 4:
                return "星期三";
            case 5:
                return "星期四";
            case 6:
                return "星期五";
            case 7:
                return "星期六";
            default:
                return "";
        }
    }


//    public static void main(String[] args) {
//        String formatOrderedDate = formatOrderedDate(System.currentTimeMillis());
//        System.out.print("___formatOrderedDate:" + formatOrderedDate);
//        String showWeek = formatDate2DateWithWeek(System.currentTimeMillis() + 24 * 60 * 60 * 1000);
//        System.out.print("___showWeek:" + showWeek);
//    }
}
